package com.atguigu.flink.chapter05.Transform;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/8/9 10:30
 */
public class Flink12_Partitioner {
    public static void main(String[] args) throws Exception {
        // 0.执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(3);

        // 1.读取数据
        //  sensor_1,123123213,10
        DataStreamSource<String> socketDS = env.socketTextStream("localhost", 9999);

        SingleOutputStreamOperator<WaterSensor> sensorDS = socketDS.map((MapFunction<String, WaterSensor>) value -> {
            String[] datas = value.split(",");
            return new WaterSensor(
                    datas[0],
                    Long.parseLong(datas[1]),
                    Integer.parseInt(datas[2])
            );
        });

        socketDS.print("input>>>");

        sensorDS.rebalance().print("rebalance>>>");

        // rescale 性能优于 rebalance
        sensorDS.rescale().print("rescale>>>");


        env.execute();
    }
}
/*
    解决某些场景的数据倾斜： 数据来源就是倾斜的。
 */